﻿using CBApp.Db.IDataAdapters.Interfaces;
using CBApp.Db.IDataAdapters.Adapters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CBApp.DataModels;
using CBApp.Models;

namespace CBApp.Controllers
{
    public class HomeController : Controller
    {
        ICBAdapter _adapter;
        public HomeController()
        {
            _adapter = new CBAdapter();
        }
        public HomeController(ICBAdapter adapter)
        {
            _adapter = adapter;
        }
        public ActionResult Index()
        {
            List<ComicBook> comicbooks = _adapter.GetAllComicBooks();
            return View(comicbooks);
        }
        public ActionResult Details(int id)
        {
            ComicBookDetailsVM model = new ComicBookDetailsVM();
            model.ComicBook = _adapter.GetComicBook(id);
            model.Artists = _adapter.GetArtistsForComicBook(id);

            return View(model);
        }
        [HttpGet]
        public ActionResult AddComicBook()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddComicBook(string title, string image)
        {
            ComicBook comicbook = new ComicBook();
            comicbook.Title = title;
            comicbook.Image = image;
            comicbook = _adapter.AddComicBook(comicbook);
            return RedirectToAction("Details/" + comicbook.Id);
        }
        [HttpGet]
        public ActionResult UpdateComicBook(int id)
        {
            ComicBook comicbook;
            comicbook = _adapter.GetComicBook(id);
            return View(comicbook);
        }
        [HttpPost]
        public ActionResult UpdateComicBook(ComicBook comicbook)
        {
            comicbook = _adapter.UpdateComicBook(comicbook);
            return RedirectToAction("Details/" + comicbook.Id);
        }
        [HttpPost]
        public ActionResult DeleteComicBook(int id)
        {
            _adapter.DeleteComicBook(id);
            return RedirectToAction("Index");
        }
    }
}